package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCartMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 鲸落
 * @Date: 2024/05/21/20:13
 * @Version: 1.0
 * @Description:
 */
@Service
public class WxCartServiceImpl_YYH implements WxCartService_YYH {
    @Autowired
    MarketCartMapper cartMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsProductMapper goodsProductMapper;
    @Override
    public List<MarketCart> goodscount(MarketUser user) {
        MarketCartExample cartExample = new MarketCartExample();
        cartExample.createCriteria().andUserIdEqualTo(user.getId());
        return cartMapper.selectByExample(cartExample);
    }

    @Override
    public Integer add(Integer goodsId, Integer number, Integer productId, MarketUser user) {
        List<MarketCart> carts = goodscount(user);
        // 如果购物车为空则直接添加
        if(carts.size() == 0){
            addOneInCart(goodsId, number, productId, user);
            return number;
        }
        // 购物车不为空，先获取当前购物车中商品的数量
        int data = 0;
        for (MarketCart cart : carts) {
            data = data+cart.getNumber();
        }
        // 查询当前购物车是否有当前规格的商品
        for (MarketCart cart : carts) {
            if(cart.getProductId().equals(productId) && cart.getGoodsId().equals(goodsId)){
                // 如果有，则更新数量
                cart.setNumber((short) (cart.getNumber()+number));
                cart.setUpdateTime(LocalDateTime.now());
                cartMapper.updateByPrimaryKeySelective(cart);
                return data+number;
            }
        }
        // 如果没有，则直接添加
        addOneInCart(goodsId, number, productId, user);
        return data+number;
    }
    // 添加商品到购物车
    private void addOneInCart(Integer goodsId, Integer number, Integer productId, MarketUser user) {
        // 根据goodsId查询goods表
        MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        // 根据productId查询product表
        MarketGoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
        // 插入购物车
        MarketCart cartRecord = new MarketCart();
        cartRecord.setUserId(user.getId());
        cartRecord.setGoodsId(goodsId);
        cartRecord.setGoodsSn(goods.getGoodsSn());
        cartRecord.setGoodsName(goods.getName());
        cartRecord.setProductId(productId);
        cartRecord.setPrice(goodsProduct.getPrice());
        cartRecord.setNumber(Short.parseShort(number +""));
        cartRecord.setSpecifications(goodsProduct.getSpecifications());
        cartRecord.setChecked(true);
        cartRecord.setPicUrl(goodsProduct.getUrl());
        cartRecord.setAddTime(LocalDateTime.now());
        cartRecord.setUpdateTime(LocalDateTime.now());
        cartRecord.setDeleted(false);
        cartMapper.insert(cartRecord);
    }

}
